﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SWCInvoice.Model;
using SWCInvoice.Returntyps;
using SWCInvoice.Enums;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Diagnostics;
using SWCInvoice.Servicelayer;

namespace SWCInvoice.Utils {
    public class Utils {
        public enum IdentifierState {
            None, Identifier, IdentifierInheritance
        };

        /// <summary>
        /// Replace the placeholder in pathnames with content
        /// </summary>
        /// <param name="name">The pathname with placeholders.</param>
        /// <returns>The replaced pathname.</returns>
        public static string PathNameReplacer(string name, string pdfName = "") {
            string result = name;

            result = result.Replace("{PDFNAME}", pdfName);
            result = result.Replace("{TIMESTAMP}", DateTime.Now.ToFileTimeUtc().ToString());
            result = result.Replace("{GUID}", Guid.NewGuid().ToString());
            result = result.Replace("{DATE}", DateTime.Now.ToString("dd-MM-yyyy"));
            result = result.Replace("{TIME}", DateTime.Now.ToString("HH-mm-ss")); 
            return result;
        }

        public static bool StartUpdater(params string[] param) {
            Process updater = new Process();
            try {
                string filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\Updater\updater.exe";
                if (!File.Exists(filePath)) {
                    string message = Properties.ResourceMain.UpdateErrorMessage;
                    string title = Properties.ResourceMain.UpdateErrorTitle;
                    MessageBox.Show(message, title,
                                System.Windows.Forms.MessageBoxButtons.OK,
                                System.Windows.Forms.MessageBoxIcon.Error);
                    return false;
                } else {
                    updater.StartInfo.FileName = filePath;
                    updater.StartInfo.Arguments = string.Join(" ", param);
                    return updater.Start();
                }
                
            } catch (Exception ex) {
                return false;
            }
        }

        
        public static void Log(string text) {
            StreamWriter sw = new StreamWriter(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\log.txt",true);            
            sw.WriteLine(string.Format("{0} - {1}",DateTime.Now.ToString(),text));
            sw.Flush();
            sw.Close();        
        }
    }
}
